#include <glib.h>

void _list_str_print_func(gpointer data, gpointer user_data)
{
    g_print("user_data is: %s, data is: %s \n", (gchar *)user_data, (gchar*)data);
}

static gpointer _list_str_copy_deep (gconstpointer value, gpointer data)
{
  return (gpointer)g_strdup((const gchar *)value);
}

gint main(gint argc, gchar **argv)
{
    GList *list = NULL;
    GList *list2 = NULL;
    GList *list3 = NULL;

    gchar str1[32] = {0};
    gchar str2[32] = {0};

    g_strlcpy(str1, "hello", sizeof("hello"));
    g_strlcpy(str2, "world", sizeof("world"));
    
    list = g_list_append(list, (gpointer)str1);
    list = g_list_append(list, (gpointer)str2);

    g_print("===test copy===\n");

    list2 = g_list_copy(list);

    g_list_foreach(list, _list_str_print_func, (gchar *)"list");
    g_list_foreach(list2, _list_str_print_func, (gchar *)"list2");

    g_strlcpy(str2, "glib", sizeof("glib"));
    
    g_list_foreach(list, _list_str_print_func, (gchar *)"list-change");
    g_list_foreach(list2, _list_str_print_func, (gchar *)"list2-change");

    g_print("===test deep copy===\n");

    list3 = g_list_copy_deep(list, _list_str_copy_deep, NULL);

    g_list_foreach(list, _list_str_print_func, (gchar *)"list");
    g_list_foreach(list2, _list_str_print_func, (gchar *)"list2");
    g_list_foreach(list3, _list_str_print_func, (gchar *)"list3");

    g_strlcpy(str1, "welcome", sizeof("welcome"));

    g_list_foreach(list, _list_str_print_func, (gchar *)"list-deep-copy");
    g_list_foreach(list2, _list_str_print_func, (gchar *)"list2-deep-copy");
    g_list_foreach(list3, _list_str_print_func, (gchar *)"list3-deep-copy");

    g_list_free(list);
    g_list_free(list2);
    g_list_free(list3);

    return 0;
}